package com.kmvc.message.activity;

import com.kmvc.message.R;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainTab extends TabActivity {

	private SharedPreferences sp;
	private Editor editor;
	private TabHost tabhost;
	public static View numView;

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		getWindow().clearFlags(1024);
		setContentView(R.layout.main_activity);
		getWindow().setBackgroundDrawable(null);

		this.tabhost = (TabHost) findViewById(android.R.id.tabhost);
		this.tabhost.setup();// 必须有，否则addTab时出错
		View localView1 = a(R.drawable.tab_icon_conversation_selector);
		numView = localView1.findViewById(R.id.unchecked_msg_num);

		View localView4 = a(R.drawable.tab_icon_setting_selector);

		Intent localIntent1 = new Intent(this, MessageList.class);
		Intent localIntent4 = new Intent(this, MessageSetting.class);
		
		sp = getSharedPreferences("com.kmvc.message_preferences", 0);
		if (sp != null & sp.getString("sizelist", "").equals("") & sp.getString("pllist", "").equals("")) {
			editor=sp.edit();
			editor.putString("sizelist", "2");
			editor.putString("pllist", "5");
			editor.commit();
		}

		this.tabhost.addTab(this.tabhost.newTabSpec("TAB1")
				.setIndicator(localView1).setContent(localIntent1));
		this.tabhost.addTab(this.tabhost.newTabSpec("TAB4")
				.setIndicator(localView4).setContent(localIntent4));

	}

	private View a(int paramInt) {
		View localView = getLayoutInflater().inflate(
				R.layout.mainactivity_tab_item, null);
		((ImageView) localView.findViewById(R.id.tab_item_image))
				.setBackgroundResource(paramInt);
		return localView;
	}

	private long exitTime = 0;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				this.exitApp();
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	/**
	 * 退出程序
	 */
	private void exitApp() {
		// 判断2次点击事件时间
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(MainTab.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			finish();
			System.exit(0);
		}
	}

	public static void SetNum(int num) {
		if (numView != null && numView instanceof TextView) {
			if (num > 0) {
				((TextView) numView).setText(num + "");
				numView.setVisibility(0);
			} else {
				numView.setVisibility(8);
			}
		}

	}

}
